package com.sinodata.bsm.cicp.collector.common;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.common.constants.DataTypeConstants;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * <p>
 * Description: DNS连接采集器
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-11-12 下午3:31:01          liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class DNSConnection extends Collector {

    private static final Logger logger = Logger.getLogger(DNSConnection.class);

    private static final String INITIAL_CONTEXT_FACTORY = "com.sun.jndi.dns.DnsContextFactory";
    private static final String AUTHORITATIVE = "false";
    private static final String DNS_RECURSION = "false";
    private static final String DNS_TIMEOUT_INITIAL = "2000";
    private static final String DNS_TIMEOUT_RETRIES = "3";

    /*
     * (non-Javadoc)
     * @see com.sinodata.bsm.cicp.collector.Collector#collect(java.lang.String)
     */
    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        ResPropertyValue propertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
        String provider_ip = this.getParamMap().get("DNS Address").trim();//DNS地址
        String check_url = this.getParamMap().get("Verify URL").trim();//验证URL
        String check_ip = this.getParamMap().get("DNS Corresponding IP").trim();//预期IP地址
        String provider_url = "dns://" + provider_ip;
        Properties env = new Properties();
        env.put(Context.AUTHORITATIVE, AUTHORITATIVE);
        env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
        env.put(Context.PROVIDER_URL, provider_url);
        env.put("com.sun.jndi.dns.recursion", DNS_RECURSION);
        env.put("com.sun.jndi.dns.timeout.initial", DNS_TIMEOUT_INITIAL);
        env.put("com.sun.jndi.dns.timeout.retries", DNS_TIMEOUT_RETRIES);
        DirContext ictx = null;
        try {
            ictx = new InitialDirContext(env);
            Attributes attrs1 = ictx.getAttributes(check_url, new String[] { "A" });
            if (attrs1 != null) {
                String returnedIP = attrs1.get("A").get().toString();
                if (check_ip.equals(returnedIP)) {
                    //propValues[0] = ResPropertyValue.createPropValue("YES", propMetas[0]);
                    propValues[0] = ResPropertyValue.createPropValue(DataTypeConstants.BOOLEAN_VALUE_TRUE, propMetas[0]);
                    propValues[1] = ResPropertyValue.createPropValue(returnedIP, propMetas[1]);
                } else {
                    // propValues[0] = ResPropertyValue.createPropValue("NO", propMetas[0]);
                    propValues[0] = ResPropertyValue.createPropValue(DataTypeConstants.BOOLEAN_VALUE_FALSE, propMetas[0]);
                    propValues[1] = ResPropertyValue.createPropValue(returnedIP, propMetas[1]);
                }
                propertyValueX.setValues(propValues);
                values.add(propertyValueX);
            }
        } catch (NamingException e) {
            logger.error("Problem searching directory:", e);
            propValues[0] = ResPropertyValue.createPropValue(e + "", propMetas[0]);
            propValues[1] = ResPropertyValue.createPropValue("Unknown", propMetas[1]);
            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
            return values;
            //            throw new CollectorException(e);
        } finally {
            if (ictx != null) {
                try {
                    ictx.close();
                } catch (NamingException e) {
                    logger.error("failed to close DirContext", e);
                }
            }
        }
        return values;
    }

    /*
     * (non-Javadoc)
     * @see com.sinodata.bsm.cicp.collector.Collector#getParamMeta()
     */
    @Override
    public String[] getParamMeta() {
        //        return new String[] { "DNS地址", "验证URL", "预期IP地址" };
        return new String[] { "DNS Address", "Verify URL", "DNS Corresponding IP" };
    }

    /*
     * (non-Javadoc)
     * @see com.sinodata.bsm.cicp.collector.Collector#getPropMeta()
     */
    @Override
    public String[] getPropMeta() {
        //        return new String[] { "是否成功", "返回IP地址" };
        return new String[] { "Whether_Successful", "Return_IP_Address" };
    }

}
